package ru.mipt.testload.util;

import com.sun.xml.xsom.*;
import org.jdesktop.swingx.JXTreeTable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;

/**
 * @author Pavel G. Belevich
 *         Date: 06.06.2010
 *         Time: 17:31:00
 */
public class XsdTreeTable extends JXTreeTable {

    private JPopupMenu popup;

    public XsdTreeTable(XsdTreeTableModel model) {
        super(model);
        this.popup = new JPopupMenu();
        this.setRootVisible(true);
        this.setRowHeight(24);
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);
            }

            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popup.removeAll();
                    int row = XsdTreeTable.this.rowAtPoint(e.getPoint());
                    TreePath path = XsdTreeTable.this.getPathForRow(row);
                    final XsdTreeTableModel model = XsdTreeTable.this.getXsdTreeTableModel();
                    if (model.getRoot() == null) {
                        Collection<XSElementDecl> xsElementDecls = model.getRootXSElementDecls();
                        for (final XSElementDecl xsElementDecl : xsElementDecls) {
                            JMenuItem menuItem = new JMenuItem(xsElementDecl.getName());
                            menuItem.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                    model.createRootElement(xsElementDecl);
                                }
                            });
                            popup.add(menuItem);
                        }
                    } else {
                        if (path != null) {
                            Object o = path.getLastPathComponent();
                            if (o instanceof Element) {
                                final Element element = (Element) o;
                                Collection<XSElementDecl> childXSElementDecls = model.getChildXSElementDeclsForElement(element);
                            }
                        }
                    }
                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
        this.setDefaultEditor(Node.class, new NodeTableCellEditor());
        this.setDefaultRenderer(XSType.class, new XSTypeTableCellRenderer());
    }

    public XsdTreeTableModel getXsdTreeTableModel() {
        return (XsdTreeTableModel) this.getTreeTableModel();
    }

}
